import {
    approval,
    createApprovalHistory,
    deleteApprovalHistoryById,
    getApprovalHistoryById,
    getChargeUpdateForPage,
    getListForPage,
    updateApprovalHistory
} from '../../services/approval/approvalHistory';
import {message} from 'antd';
import Constants from '../../utils/Constants'

const R = require('ramda');

export default {
    namespace: 'approvalHistory',
    state: {
        [Constants.BUSINESSTYPE.CHARGECONFIRM + "Page"]: {},
        [Constants.BUSINESSTYPE.CHARGECONFIRM + "List"]: [],
        [Constants.BUSINESSTYPE.CHARGE_ADD + "Page"]: {},
        [Constants.BUSINESSTYPE.CHARGE_ADD + "List"]: [],
        [Constants.BUSINESSTYPE.CHARGE_UPDATE + "Page"]: {},
        [Constants.BUSINESSTYPE.CHARGE_UPDATE + "List"]: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         * 
         * @param state 
         */
        refreshState(state, { payload }) {
            return { ...state, ...payload };
        }
    },
    effects: {
        /**
         * 分页查询审批历史
         * 
         * @param entityId
         * @param businessType
         * @param page
         */
        *getListForPage({ payload }, { call, put }) {
            const res = yield call(getListForPage, { ...R.omit(['onSuccess', 'onFail', 'key'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        [payload.key + 'List']: res.data.content.list,
                        [payload.key + 'Page']: res.data.content.page
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 分页查询费用变更历史
         * 
         * @param entityId
         * @param businessType
         * @param page
         */
        *getChargeUpdateForPage({ payload }, { call, put }) {
            const res = yield call(getChargeUpdateForPage, { ...R.omit(['onSuccess', 'onFail', 'key'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        [payload.key + 'List']: res.data.content.list,
                        [payload.key + 'Page']: res.data.content.page
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
	     * 根据id获取审批历史
	     *
	     * @param id
	     * @return
	     */
        *getApprovalHistoryById({ payload }, { call, put }) {
            if (payload.id) {
                const res = yield call(getApprovalHistoryById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },
	    /**
	     * 新增审批历史
	     *
	     * @param approvalHistoryForm
         * @param bindingResult
	     * @return
	     */
        *createApprovalHistory({ payload }, { call }) {
            const res = yield call(createApprovalHistory, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("提交审批成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
	     * 更新审批历史
	     *
	     * @param approvalHistoryForm
	     * @param bindingResult
	     * @return
	     */
        *updateApprovalHistory({ payload }, { call, put }) {
            const res = yield call(updateApprovalHistory, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
	    /**
	     * 审批
	     *
	     * @param approvalHistoryForm
	     * @param bindingResult
	     * @return
	     */
        *approval({ payload }, { call, put }) {
            const res = yield call(approval, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("审批成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
	     * 根据id删除审批历史
	     *
	     * @param id
	     * @return
	     */
        *deleteApprovalHistoryById({ payload }, { call }) {
            const res = yield call(deleteApprovalHistoryById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("删除成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        }
    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};